package com.zmj.sy.mom.srv.aps.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Station;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.Result;
import com.zmj.sy.mom.srv.aps.bean.vo.weldingUploadConfig.StationDistributeVo;
import com.zmj.sy.mom.srv.aps.mapper.StationMapper;
import com.zmj.sy.mom.srv.aps.utils.PageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.zmj.sy.mom.srv.aps.utils.ApsConstant.MES_STATION_DISTRIBUTE;

@RequiredArgsConstructor
@Service
public class WeldingUploadConfigService {

    private final StringRedisTemplate stringRedisTemplate;
    private final StationMapper stationMapper;
    /**
     * 焊接工位手动动态分配
     *
     * @author hejiahui
     * @since 2022年10月15日15:54:10
     */
    public void dynamicDistributeStation(String station, String[] phase) throws Exception {
        ValueOperations<String, String> valOperation = stringRedisTemplate.opsForValue();
        // 将新的分配信息用,隔开，存入redis中。
        if (phase != null && phase.length != 0) {
            String phases = phase[0];
            for (int i = 1; i < phase.length; i++) {
                phases = phases + "," + phase[i];
            }
            valOperation.set(MES_STATION_DISTRIBUTE + station, phases);
        } else {
            valOperation.set(MES_STATION_DISTRIBUTE + station, "");
        }
    }
    /**
     * @Description: 查询焊接工位手动动态分配结果
     *
     * @author: hejiahui
     * @since: 2024/10/6 10:59
     */
    public Result getDistributeStationResult(String group, String workStation, Long pageSize, Long pageNo) throws Exception {
        ValueOperations<String, String> valOperation = stringRedisTemplate.opsForValue();
        // 获得工作组中所有的焊接工位，返回给前端
        List<String> stationList = this.getWorkStation(group);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("stationList", stationList);
        // 如果station不为空，则只返回这个工位的数据。
        List<StationDistributeVo> distributeVos = new ArrayList<>();
        if (workStation != null && !workStation.isEmpty()) {
            String phases = valOperation.get(MES_STATION_DISTRIBUTE + workStation);
            StationDistributeVo distributeVo = new StationDistributeVo();
            if (phases == null || phases.isEmpty()) {
                distributeVo.setPhases(null);
            } else {
                List<String> phaseList = Arrays.asList(phases.split(","));
                distributeVo.setPhases(phaseList);
            }
            distributeVo.setGroup(workStation.substring(0, 3));
            distributeVo.setStation(workStation);
            distributeVos.add(distributeVo);
            PageUtils<StationDistributeVo> pageUtils = new PageUtils(pageNo, pageSize, distributeVos);
            resultMap.put("distributeResult", pageUtils);
            return Result.success(resultMap);
        } else {
            // 循环查到各个工位的分配结果，并且按,分开存放的list中，将工位的分配结果分页显示。
            for (String station : stationList) {
                String phases = valOperation.get(MES_STATION_DISTRIBUTE + station);
                StationDistributeVo distributeVo = new StationDistributeVo();
                if (phases == null || phases.isEmpty()) {
                    distributeVo.setGroup(station.substring(0, 3));
                    distributeVo.setStation(station);
                    distributeVo.setPhases(null);
                    distributeVos.add(distributeVo);
                    continue;
                }
                List<String> phaseList = Arrays.asList(phases.split(","));
                distributeVo.setGroup(station.substring(0, 3));
                distributeVo.setStation(station);
                distributeVo.setPhases(phaseList);
                distributeVos.add(distributeVo);
            }
            PageUtils<StationDistributeVo> pageUtils = new PageUtils(pageNo, pageSize, distributeVos);
            resultMap.put("distributeResult", pageUtils);
            return Result.success(resultMap);
        }
    }
    /**
     * 获得工作组下的所有工作站
     *
     * @author hejiahui
     * @since 2022年10月15日15:54:10
     */
    private List<String> getWorkStation(String group) {
        // 如果不传工作组号，则回显所有焊接工位的分配情况。
        List<Station> workStationList = new ArrayList<>();
        if (group == null || group.isEmpty()) {
            QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
            queryWrapper.and(e -> {
                e.like("my_code", "3-1_SC_ZHJT").or().like("my_code", "3-2_SC_ZHJT").or().like("my_code", "3-3_SC_ZHJT");
            });
            List<Station> stationList = stationMapper.selectList(queryWrapper);
            workStationList = stationList;
        } else {
            List<Station> stationList = stationMapper.lambdaQuery().like(Station::getMyCode, group + "_SC_ZHJT").list();
            workStationList = stationList;
        }
        List<String> workStations = new ArrayList<>();
        for (Station station : workStationList) {
            workStations.add(station.getMyCode());
        }
        return workStations;
    }
}
